<?php

namespace app\foreigndata\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $pubKey = 'weacb2c7cfc';

    //
    public function getShopInfo()
    {

        $mobile = $this->request->post('mobile');
        $shop_id = $this->request->post('shop_id');
        $gameName = $this->request->post('name');
        $cate_type=$this->request->post('cate_type');
        $type_id=$this->request->post('type_id');
        $plat=$this->request->post('plat');
        $server=$this->request->post('server');
        $area=$this->request->post('area');
        $operator=$this->request->post('operator');
        $title = $this->request->post('title');
        $price = $this->request->post('price');
        $type = $this->request->post('type');
        $images = $this->request->post('images');
        $content = $this->request->post('content');
        $sign = $this->request->post('sign');
        dump($this->request->post());die;
        $sign_verify = md5($mobile.$this->pubKey);
        if(!$sign || $sign != $sign_verify){
            $this->error("验证失败，请重试~");
        }

        if(!$shop_id){
            $this->error("商品id不能为空");
        }
        if(!$gameName){
            $this->error("商品名称不能为空");
        }
        if(!$title){
            $this->error("商品标题不能为空");
        }
        if(!$price){
            $this->error("商品价格不能为空");
        }else if(!preg_match('/^[1-9]+\d*(.\d{1,2})?$|^\d+.\d{1,2}$/',$price)){
//            '/\d*(.\d{1,2})?/'
            $this->error("价格格式不正确");
        }

        $game_id = 0;
        switch ($gameName){
            case "王者荣耀":
                $game_id = 1;
                break;
            case "和平精英";
                $game_id = 6;
                break;
            case "原神";
                $game_id = 5;
                break;
            case "永劫无间";
                $game_id = 6;
                break;
            case "英雄联盟手游";
                $game_id = 4;
                break;
            case "穿越火线";
                $game_id = 11;
                break;
//            case "地下城与勇士";
//                $game_id = 2;
//                break;
//            case "流放之路";
//                $game_id = 18;
//                break;
//            case "剑侠情缘3";
//                $game_id = 20;
//                break;
//            case "剑灵";
//                $game_id = 21;
//                break;
//            case "幻塔";
//                $game_id = 26;
//                break;
            default:
                $game_id = 0;
                break;
        }
        if($game_id == 0){
            //本地未找到游戏，加入日志，提示运维加入游戏
            $this->writeLog("未找到游戏" . $gameName);
            $this->error("未找到此游戏");
        }

        //查询分类
        $game_type = DB::name("game_info")->where(['game_id'=>$game_id,'gt_id'=>1])->select();
//        if(!$game_type){
//
//        }
//        if(strpos($))




        $data = [
            'shop_id'   =>  $shop_id,
//            'gameid'    =>  $gameId,
            'game'    =>  $gameName,
            'content'    =>  $content,
            'image'   =>  $images,
            'cate_type' =>  $cate_type,
            'title' =>  $title,
            'price'  =>  $price,
//            'plat_id' =>  $plat_id,
            'plat'    =>  $plat,
//            'operator_id'    =>  $operator_id,
            'operator'   =>  $operator,
//            'area_id'    =>  $area_id,
            'area' =>  $area,
//            'server_id'    =>  $server_id,
            'server'    =>  $server,
//            'cross_id'    =>  $cross_id,
//            'cross'    =>  $cross,
            'content' =>  $content,
            'mobile'  =>  $mobile,
            'type'    =>  $type,
            'type_id'    =>  $type_id,
            'create_time'    =>  date("Y-m-d H:i:s",time()),
        ];

        $res = DB::name("hhzo_game")->where("shop_id",$shop_id)->find();
        if($res){
            $ress = DB::name("hhzo_game")->where("shop_id",$shop_id)->update($data);
        }else{
            $ress = DB::name("hhzo_game")->insert($data);
        }

        if($ress){
            $this->success('商品编辑成功');
        }else{
            $this->error("商品添加失败");
        }



    }

    public function testSign(){
        $mobile = $this->request->post("mobile");
        return md5($mobile.$this->pubKey);
    }

}
